<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Points as CommonPoints;

/**
 * 积分管理逻辑
 */
class Points extends AdminBase
{
    
    public static $commonPointsLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonPointsLogic) && static::$commonPointsLogic = new CommonPoints();
    }
    
    /**
     * 获取积分列表搜索条件
     * @param array $data 搜索参数
     * @return array
     */
    public function getWhere($data = [])
    {
        return static::$commonPointsLogic->getWhere($data);
    }
    
    /**
     * 获取积分列表
     */
    public function getPointsList($where = [], $field = true, $order = 'create_time desc', $paginate = DB_LIST_ROWS)
    {
        return static::$commonPointsLogic->getPointsList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取积分信息
     */
    public function getPointsInfo($where = [], $field = true)
    {
        return static::$commonPointsLogic->getPointsInfo($where, $field);
    }
    
    /**
     * 积分调整
     */
    public function pointsAdjust($data = [])
    {
        $url = url('index');
        
        $validate_result = $this->validatePoints->scene('adjust')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validatePoints->getError()];
        }
        
        $result = static::$commonPointsLogic->pointsAdjust($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('调整', '积分调整，用户ID：' . $data['user_id'] . '，积分：' . $data['points']);
        
        return [RESULT_SUCCESS, '积分调整成功', $url];
    }
    
    /**
     * 积分规则设置
     */
    public function pointsRules($data = [])
    {
        $url = url('rules');
        
        $result = static::$commonPointsLogic->pointsRules($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('设置', '积分规则设置更新');
        
        return [RESULT_SUCCESS, '设置保存成功', $url];
    }
    
    /**
     * 获取积分规则
     */
    public function getPointsRules()
    {
        return static::$commonPointsLogic->getPointsRules();
    }
    
    /**
     * 获取积分统计
     */
    public function getPointsStats($data = [])
    {
        return static::$commonPointsLogic->getPointsStats($data);
    }
    
    /**
     * 导出积分列表
     */
    public function exportPointsList($where = [], $field = true, $order = 'create_time desc')
    {
        $list = static::$commonPointsLogic->getPointsList($where, $field, $order, false);
        
        $titles = "用户ID,积分变动,类型,备注,创建时间";
        $keys   = "user_id,points,type_text,remark,create_time";
        
        action_log('导出', '导出积分列表');
        
        export_excel($titles, $keys, $list, '积分列表');
    }
    
    /**
     * 积分商城设置
     */
    public function pointsMall($data = [])
    {
        $url = url('mall');
        
        $result = static::$commonPointsLogic->pointsMall($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('设置', '积分商城设置更新');
        
        return [RESULT_SUCCESS, '设置保存成功', $url];
    }
    
    /**
     * 获取商城商品列表
     */
    public function getMallList($where = [], $field = true, $order = 'sort desc', $paginate = DB_LIST_ROWS)
    {
        return static::$commonPointsLogic->getMallList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取兑换记录搜索条件
     */
    public function getExchangeWhere($data = [])
    {
        $where = [];
        
        !empty($data['user_id']) && $where['user_id'] = $data['user_id'];
        !empty($data['status']) && $where['status'] = $data['status'];
        !empty($data['start_time']) && $where['create_time'] = ['>=', strtotime($data['start_time'])];
        !empty($data['end_time']) && $where['create_time'] = ['<=', strtotime($data['end_time'])];
        !empty($data['search_data']) && $where['user_id|order_no'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 获取兑换记录列表
     */
    public function getExchangeList($where = [], $field = true, $order = 'create_time desc', $paginate = DB_LIST_ROWS)
    {
        return static::$commonPointsLogic->getExchangeList($where, $field, $order, $paginate);
    }
    
    /**
     * 兑换审核
     */
    public function exchangeReview($data = [])
    {
        $url = url('exchange');
        
        $validate_result = $this->validatePoints->scene('exchange_review')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validatePoints->getError()];
        }
        
        $result = static::$commonPointsLogic->exchangeReview($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('审核', '兑换审核，id：' . $data['id']);
        
        return [RESULT_SUCCESS, '审核成功', $url];
    }
}